﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace OpenPhotoAlbum.Controls
{
    public class TimePointDescriber
    {
        private string _formatYears;
        private string _formatYear;
        private string _formatMonths;
        private string _formatMonth;
        private string _formatWeeks;
        private string _formatWeek;
        private string _formatDays;
        private string _formatDay;
        private string _formatHours;
        private string _formatHour;
        private string _formatMinutes;
        private string _formatJustNow;

        private const int THRESHOLD_JUST_NOW = 5;

        public TimePointDescriber(
            string formatYears,
            string formatYear,
            string formatMonths,
            string formatMonth,
            string formatWeeks,
            string formatWeek,
            string formatDays,
            string formatDay,
            string formatHours,
            string formatHour,
            string formatMinutes,
            string formatJustNow)
        {
            _formatYears = formatYears;
            _formatYear = formatYear;
            _formatMonths = formatMonths;
            _formatMonth = formatMonth;
            _formatWeeks = formatWeeks;
            _formatWeek = formatWeek;
            _formatDays = formatDays;
            _formatDay = formatDay;
            _formatHours = formatHours;
            _formatHour = formatHour;
            _formatMinutes = formatMinutes;
            _formatJustNow = formatJustNow;
        }

        public string Describe(DateTime timePoint)
        {
			return Describe(timePoint, DateTime.UtcNow);
        }

        public string Describe(DateTime timePoint, DateTime now)
        {
            if (timePoint.CompareTo(now) > 0) { throw new InvalidTimePointException(); }

            TimeSpan t = now.Subtract(timePoint);
            if (t.TotalDays >= 365 * 2) { return string.Format(CultureInfo.CurrentCulture, _formatYears, (int)t.TotalDays / 365); }
            else if (t.TotalDays >= 365) { return string.Format(CultureInfo.CurrentCulture, _formatYear, 1); }
			else if (t.TotalDays >= 30 * 2) { return string.Format(CultureInfo.CurrentCulture, _formatMonths, (int)t.TotalDays / 30); }
            else if (t.TotalDays >= 30) { return string.Format(CultureInfo.CurrentCulture, _formatMonth, 1); }
			else if (t.TotalDays >= 7 * 2) { return string.Format(CultureInfo.CurrentCulture, _formatWeeks, (int)t.TotalDays / 7); }
            else if (t.TotalDays >= 7) { return string.Format(CultureInfo.CurrentCulture, _formatWeek, 1); }
			else if (t.TotalDays >= 2) { return string.Format(CultureInfo.CurrentCulture, _formatDays, (int)t.TotalDays); }
            else if (t.TotalDays >= 1) { return string.Format(CultureInfo.CurrentCulture, _formatDay, 1); }
			else if (t.TotalHours >= 2) { return string.Format(CultureInfo.CurrentCulture, _formatHours, (int)t.TotalHours); }
            else if (t.TotalHours >= 1) { return string.Format(CultureInfo.CurrentCulture, _formatHour, 1); }
			else if (t.TotalMinutes >= THRESHOLD_JUST_NOW) { return string.Format(CultureInfo.CurrentCulture, _formatMinutes, (int)t.TotalMinutes); }
            else { return _formatJustNow; }
        }
    }

    public class InvalidTimePointException : Exception { }
}
